home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_315 / surf / tov3d.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  9KB  |  365 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define true 1
  5. #define false 0
  6.  
  7. /*
  8.  * types
  9.  */
  10. typedef float triplet[3];
  11. typedef int  quadrlat[4];
  12.  
  13. typedef enum {
  14.                TminX, TminY, TminZ,
  15.                TmaxX, TmaxY, TmaxZ,
  16.                Tcolorin, Tcolorout, Tnameout } ArgType;
  17.  
  18. extern char *malloc();
  19. extern double atof();
  20.  
  21. char *ArgName[] = {
  22.     "minx", "miny", "minz", "maxx", "maxy", "maxz", "ci", "co", "o", NULL
  23.   };
  24.  
  25. /*
  26.  * command line arguments
  27.  */
  28. triplet MinIn, MaxIn; /* define bounding box */
  29. char MinSet[3] = {false}, MaxSet[3] = {false};
  30.  
  31. int colorin = -1,
  32.     colorout = -1;
  33. char *namein = NULL,
  34.      *nameout = NULL;
  35. /*
  36.  * file arguments
  37.  */
  38. FILE *infile, *outfile= NULL;
  39. int linenum;
  40.  
  41. /*
  42.  * mucky global variables
  43.  */
  44. triplet *inring;
  45. char *indlist;
  46. int pnt_total; /* total number of points */
  47. triplet Scale = { 1.0, 1.0, 1.0 };
  48. triplet Disp = { 0.0, 0.0, 0.0 };
  49.  
  50.  
  51. int NumRings, /* number of curved rings */
  52.     PtsInPerRing, /* points read in per ring */
  53.     PtsOutPerRing, /* points written per ring */
  54.     IsClosed; /* true if revolution is 360 degrees */
  55.  
  56. /*
  57.  * parse the command line arguments
  58.  */
  59. void getCliArgs(argc, argv)
  60.     int argc;
  61.     char *argv[];
  62. {
  63.     int i;
  64.     for( i = 0; i <argc; i++ ) {
  65.         if( *argv[i] == '-' ) {
  66.             int j;
  67.  
  68.             for( j = 0; ArgName[j]; j++ ) {
  69.                 if( !strncmp(ArgName[j], argv[i]+1, strlen(ArgName[j])))
  70.                     break;
  71.             }
  72.  
  73.             if( !ArgName[j] ) {
  74.                 fprintf(stderr,"%s unknown, try one of below:\n", argv[i]);
  75.                 for( j = 0; ArgName[j]; j++ ) {
  76.                     fprintf(stderr,"-%s val\n", ArgName[j] );
  77.                 }
  78.                 exit(-1);
  79.             }
  80.             else {
  81.                 char *argpos;
  82.                 register int element;
  83.  
  84.                 if( strlen(argv[i]+1) > strlen(ArgName[j]) ) {
  85.                     argpos = argv[i] + strlen(ArgName[j]) +1;
  86.                 }
  87.                 else {
  88.                     i++;
  89.                     if( i >= argc ) {
  90.                         fprintf(stderr,"missing arg for -%s\n", argv[i-1] );
  91.                         exit(-1);
  92.                     }
  93.                     argpos = argv[i];
  94.                 }
  95.                 switch( (ArgType)j ) {
  96.                     case TminX:
  97.                     case TminY:
  98.                     case TminZ:
  99.                         element = j - (int)TminX;
  100.                         MinIn[element] = atof(argpos);
  101.                         MinSet[element] = true;
  102.                         break;
  103.                     case TmaxX:
  104.                     case TmaxY:
  105.                     case TmaxZ:
  106.                         element = j - (int)TmaxX;
  107.                         MaxIn[element] = atof(argpos);
  108.                         MaxSet[element] = true;
  109.                         break;
  110.                     case Tcolorin: colorin = atoi(argpos); break;
  111.                     case Tcolorout: colorout = atoi(argpos); break;
  112.                     case Tnameout: nameout = argpos;
  113.                 } /* end case */
  114.             }
  115.         }
  116.         else {
  117.                 namein = argv[i];
  118.         }
  119.     } /* end for loop */
  120.  
  121.     if (!namein ) {
  122.         fprintf(stderr,"No input file named\n");
  123.         exit(-1);
  124.     }
  125.     if(!nameout) {
  126.         nameout = (char *)malloc(200); /* replace with max path size */
  127.         sprintf(nameout, "%s.v3d", namein);
  128.     }
  129. }
  130.  
  131. /*
  132.  * read an integer number into variable
  133.  */
  134. void readInt(IntVar)
  135.     int *IntVar;
  136. {
  137.     char c;
  138.     linenum++;
  139.     if( !fscanf(infile, "%d", IntVar)) {
  140.         fprintf(stderr, "could not read integer at line %d\n", linenum);
  141.         exit(-1);
  142.     }
  143.     /*
  144.      * skip to end of line
  145.      */
  146.     while( c = getc(infile), c != EOF && c != '\n' ) ;
  147. }
  148.  
  149. void readTriplet( tripvar )
  150.     triplet tripvar;
  151. {
  152.     linenum++;
  153.     if(!fscanf(infile, "%f %f %f",tripvar, tripvar+1, tripvar+2)) {
  154.         fprintf(stderr,"could not read coordinate triplet at line %d\n",
  155.             linenum);
  156.         exit(-1);
  157.     }
  158. }
  159. /*
  160.  * read an entire ring
  161.  */
  162. void readRing() {
  163.     int i;
  164.     for( i = 0; i< PtsInPerRing; i++ ) {
  165.         readTriplet(inring[i]);
  166.     }
  167. }
  168.  
  169. /*
  170.  * read the first few lines in the file that tell us how many
  171.  * rings, howmany points per ring, etc
  172.  */
  173. void readHeader() {
  174.     int RevRange;
  175.     int RevMesh;
  176.  
  177.     linenum = 0;
  178.     readInt( &NumRings );
  179.     readInt( &RevMesh );
  180.     readInt( &RevRange );
  181.  
  182.     PtsInPerRing = RevMesh + 1;
  183.     if( RevRange == 360 ) {
  184.         IsClosed = true;
  185.         PtsOutPerRing = RevMesh;
  186.     }
  187.     else {
  188.         IsClosed = false;
  189.         PtsOutPerRing = PtsInPerRing;
  190.     }
  191. }
  192. /*
  193.  * this procedure reads in a list of data points to determine the
  194.  * following:
  195.  *      total number of data points to write out
  196.  *      maximun x,y,z spread
  197.  */
  198. void readPass1() {
  199.     triplet max3, min3;
  200.     int i;
  201.     pnt_total = 0;
  202.  
  203.     inring = (triplet *)malloc(sizeof(triplet)*PtsInPerRing);
  204.     indlist = (char *)malloc(sizeof(char)*NumRings);
  205.  
  206.     for( i = 0; i < NumRings; i++ ) {
  207.         int j;
  208.         readRing();
  209.  
  210.         for( j = 0; j < PtsOutPerRing; j++ ) {
  211.             int k;
  212.             for( k = 0; k < 3; k++ ) {
  213.                 if( max3[k] < inring[j][k] || !i && !j ) max3[k] = inring[j][k];
  214.                 if( min3[k] > inring[j][k] || !i && !j ) min3[k] = inring[j][k];
  215.             }
  216.         }
  217.         if( inring[0][0] == inring[1][0] &&
  218.             inring[0][1] == inring[1][1] ) {
  219.             indlist[i] = 0;
  220.             pnt_total += 1;
  221.         }
  222.         else {
  223.             indlist[i] = 1;
  224.             pnt_total += PtsOutPerRing;
  225.         }
  226.     }
  227.     /*
  228.      * compute scaling and displacement
  229.      */
  230.     for( i = 0; i < 3; i++ ) {
  231.         if( MaxSet[i]  && !MinSet[i] )
  232.             MinIn[i] = -MaxIn[i];
  233.         else if( !MaxSet[i] && MinSet[i] )
  234.             MaxIn[i] = -MinIn[i];
  235.  
  236.         Scale[i] = 1.0;
  237.         Disp[i] = 0.0;
  238.         if( MaxSet[i] || MinSet[i] ) {
  239.             float diffOut, diffIn;
  240.  
  241.             diffOut = MaxIn[i] - MinIn[i];
  242.             diffIn = max3[i] - min3[i];
  243.             if( diffIn > 0 ) Scale[i] = diffOut/diffIn;
  244.             Disp[i] = MaxIn[i] - max3[i] * Scale[i];
  245.         }
  246.     }
  247. }
  248.  
  249. void writeTriplet(trip)
  250.     triplet trip;
  251. {
  252.     int i;
  253.     for( i = 0; i < 3; i++ ) {
  254.         fprintf(outfile,"%f%c", trip[i]*Scale[i]+Disp[i], i==2?'\n':' ');
  255.     }
  256. }
  257.  
  258.  
  259. void writeRing()
  260. {
  261.     int i;
  262.     for( i = 0; i < PtsOutPerRing; i++ ) {
  263.         writeTriplet(inring[i]);
  264.     }
  265. }
  266.  
  267.  
  268.  
  269. void writePoints()
  270. {
  271.     int ringno;
  272.  
  273.     fprintf(outfile, "%d\n", pnt_total);
  274.     for( ringno = 0; ringno < NumRings; ringno++ ) {
  275.         readRing();
  276.         if( indlist[ringno] )
  277.             writeRing();
  278.         else
  279.             writeTriplet( inring[0] );
  280.     }
  281. }
  282.  
  283.  
  284. void writeQuad(quad, numpts, color, order)
  285.     quadrlat quad;
  286.     int numpts, color, order;
  287. {
  288.     int i;
  289.     fprintf(outfile, "%d ", numpts);
  290.     for(i=0; i <numpts; i++ ) {
  291.         fprintf(outfile,"%d ", quad[order?i:(numpts-i -1)]);
  292.     }
  293.     fprintf(outfile,"%d \n", color);
  294. }
  295.  
  296. void writePolys() {
  297.     int RevA, RevB;
  298.     int PtCntA, PtCntB;
  299.     int RingA, RingB;
  300.     int PolysPerRing;
  301.  
  302.     PolysPerRing = PtsInPerRing -1;
  303.     PtCntB = RingA = 0;
  304.     for( RingB =1; PtCntA = PtCntB, RingB < NumRings; RingA = RingB++ ) {
  305.  
  306.         PtCntB += indlist[RingA]? PtsOutPerRing: 1;
  307.         for( RevA =0; RevA < PolysPerRing; RevA++ ) {
  308.             quadrlat quad;
  309.             int qcnt;
  310.  
  311.             RevB = RevA +1;
  312.             if( RevB >= PtsOutPerRing ) RevB = 0;
  313.  
  314.             qcnt = 0;
  315.             if(indlist[RingA] ) {
  316.                 quad[qcnt++] = PtCntA + RevA;
  317.                 quad[qcnt++] = PtCntA + RevB;
  318.             }
  319.             else
  320.                 quad[qcnt++] = PtCntA;
  321.  
  322.             if(indlist[RingB] ) {
  323.                 quad[qcnt++] = PtCntB + RevB;
  324.                 quad[qcnt++] = PtCntB + RevA;
  325.             }
  326.             else
  327.                 quad[qcnt++] = PtCntB;
  328.  
  329.             if( qcnt >= 3 ) {
  330.                 if( colorin >= 0 ) writeQuad(quad, qcnt, colorin, false);
  331.                 if( colorout >=0 ) writeQuad(quad, qcnt, colorout, true);
  332.             }
  333.         }
  334.     }
  335. }
  336.  
  337. main(argc, argv)
  338.     int argc;
  339.     char *argv[];
  340. {
  341.     getCliArgs(argc, argv);
  342.     infile = fopen(namein, "r");
  343.     if(!infile) {
  344.         fprintf(stderr,"can not read \"%s\"\n", namein);
  345.         exit(-1);
  346.     }
  347.  
  348.     readHeader();
  349.     readPass1();
  350.     fclose(infile);
  351.  
  352.     outfile = fopen(nameout, "w");
  353.     if( !outfile) {
  354.         fprintf(stderr, "can not write to \"%s\"\n", nameout);
  355.         exit(-1);
  356.     }
  357.  
  358.     infile = fopen(namein, "r");
  359.     readHeader();
  360.     writePoints();
  361.     fclose(infile);
  362.     writePolys();
  363.     fclose(outfile);
  364. }
  365.